package com.jianglin.state2.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
import com.jianglin.state2.core.ResponsePack;
import com.jianglin.state2.dao.entity.Employee;
import com.jianglin.state2.model.core.PageParams;
import com.jianglin.state2.model.params.EmployeeAddParams;
import com.jianglin.state2.model.params.EmployeeUpdateParams;
import com.jianglin.state2.service.EmployeeService;
import com.jianglin.state2.util.SqlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@ResponsePack
@RequestMapping("/employee")
@Api(tags = {"员工用户"})
public class EmployeeController {

    private final EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @GetMapping("/all")
    @ApiOperation("获取所有员工")
    public List<Employee> getAll() {
        return employeeService.list();
    }


    @GetMapping("/page")
    public List<Employee> getPage(@Validated PageParams pageParams){
        return employeeService.page(SqlUtil.getPage(pageParams)).getRecords();
    }

    @GetMapping("/employeeId/{employeeId}")
    @ApiOperation("根据员工ID查找员工")
    public Employee getEmployeeById(@ApiParam("员工ID") @PathVariable Integer employeeId){
        return employeeService.listByMap(SqlUtil.getColumnMap("employeeId",employeeId)).get(0);
    }

    @GetMapping("/count")
    @ApiOperation("获取员工数")
    public Integer getCount(){
        return employeeService.count();
    }

    @GetMapping("/employeeForm/{employeeForm}")
    @ApiOperation("根据员工类别查找员工")
    public List<Employee> getEmployeeByForm(@ApiParam("员工类型") @PathVariable Integer employeeForm){
        return employeeService.listByMap(SqlUtil.getColumnMap("employeeForm",employeeForm));
    }

    @PostMapping("/add")
    @ApiOperation("添加员工")
    public boolean saveEmployee(@Validated @RequestBody EmployeeAddParams employeeAddParams) {
        Employee employee = employeeAddParams.convertOut();
        return employeeService.save(employee);
    }


    @PutMapping("/{id}")
    @ApiOperation("更新员工信息")
    public boolean updateEmployee(@ApiParam("员工编号") @PathVariable Integer id,
                                  @Validated @RequestBody EmployeeUpdateParams employeeUpdateParams){
        Employee employee = employeeUpdateParams.convertOut();
        employee.setEmployeeId(id);
        return employeeService.updateById(employee);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除员工")
    public boolean deleteEmployee(@ApiParam("员工编号") @PathVariable String id) {
        return employeeService.removeById(id);
    }
}
